<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\AdminRule;

use think\Loader;

	/**
	 * 后台管理权限管理
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 */

class Rule extends AdminBase
{

	/**
	 * 权限列表
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 */
	public function index(){
		//实例化
		$nav = new AdminRule();

		//查询所有数据
		$data = $nav->selectData([],'*','sort desc,id asc');

		//引入拓展类
		Loader::import('Org.Arraysets',EXTEND_PATH);

		//实例化
		$Arraysets = new \Arraysets;

		//按照树的形式输出
		$data = $Arraysets->getTree($data);

		//判断是否有子级
		foreach ($data as $key => $value) {
			$data[$key]['child'] = $Arraysets->haveChild($value['id'],$data,'pid');
		}

		$this->assign('data',$data);
		return view('index');
	}

	/**
	 * 添加权限列表
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 * @param  string  $name 权限名
	 * @param  string  $mac  连接名
	 * @param  string  $pid  父级权限ID(可选)(添加子权限必选)
	 */
	public function add(){
		//判断post提交
		if(request()->isPost()){

			//获取数据
			$name = input('post.name','');
			$mac  = input('post.mac','');
			$pid  = input('post.pid',0);
			if(empty($name) || empty($mac)){
				$this->result([],-1,'参数错误,请刷新页面再试','json');
			}

			//实例化
			$nav = new AdminRule();
			//数据存储
			$data = [
				'name' => trim($name),
				'mac'  => trim($mac),
				'pid'  => $pid
			];

			//添加  返回执行条数/ID
			$op = $nav->addData($data);
			if($op){
				$this->result($op,200,'成功','json');
			}
			$this->result($op,-1,'失败','json');
		}
		$pid = input('param.pid',0);
		$this->assign('pid',$pid);
		return view('add');
	}

	/**
	 * 修改权限列表
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 * @param  string  $name 权限名
	 * @param  string  $mac  连接名
	 * @param  string  $pid  父级权限ID(可选)(添加子权限必选)
	 */
	public function edit(){
		//实例化
		$nav = new AdminRule();
		//判断post提交
		if(request()->isPost()){

			//获取数据
			$id = input('post.id','');
			$name = input('post.name','');
			$mac  = input('post.mac','');
			if(empty($id) || empty($name) || empty($mac)){
				$this->result([],-1,'参数错误,请刷新页面再试','json');
			}
			//数据存储
			$data = [
				'name' => trim($name),
				'mac'  => trim($mac),
			];

			//添加  返回执行条数/ID
			$op = $nav->saveData($data,['id' => trim($id)]);
			if($op){
				$this->result($op,200,'成功','json');
			}
			$this->result($op,-1,'失败','json');
		}
		$id = input('param.id');
		//查询这条数据
		$finddata = $nav->findData($id);
		$this->assign('data',$finddata);
		return view('edit');
	}

	/**
	 * 删除权限
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 * @param  int $id  权限ID
	 */
	public function del(){
		//获取参数
		$id = input('param.id','');
		if(empty($id)){
			$this->result([],-1,'参数错误,请刷新页面再试','json');
		}

		//验证数据是否正确

		//实例化
		$nav = new AdminRule();

		//查询这条数据
		$finddata = $nav->findData($id,'id');
		if(empty($finddata)){
			$this->result([],-1,'参数错误:这条数据不存在或者已经被删除','json');
		}

		//查看该数据是否拥有子级
		$finddata_child = $nav->findData(['pid'=>$id],'id');
		if(!empty($finddata_child)){
			$this->result([],-1,'该级权限下还有子级权限,不能删除','json');
		}

		//删除数据
		$result = $nav->deleteData($id);
		if($result){
			$this->result($result,200,'已删除','json');
		}
		$this->result([],-1,'删除失败：请刷新后再试','json');
	}

	/**
	 * 权限排序
	 * @author Yel 
	 * @copyright  yel  2019/6/28
	 * @param  stirng $data  son字符串：如{'0':{'id':$id,'sort':$sort},'1':{'id':$id,'sort':$sort}}
	 */
	public function saveSort(){
		//获取参数
		$data = input('param.data','');
		if(empty($data)){
			$this->result([],-1,'参数错误,请刷新页面再试','json');
		}
		$data_array = json_decode($data,true);

		//更新数据
		//实例化
		$nav = new AdminRule();
		$result = $nav->saveAllData($data_array);
		if($result){
			$this->result(true,200,'保存成功','json');
		}
		$this->result(true,-1,'保存失败','json');
	}
}